package cn.edu.java.io.outputstream.filteroutputstream;

import java.io.*;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;

public class CheckedOutputStreamDemo {
    public static void main(String[] args) {
        String data = "Hello, World!";
        String outputFile = "output/output.txt";

        try {
            // 创建文件输出流
            FileOutputStream fos = new FileOutputStream(outputFile);

            // 创建校验和输出流
            CheckedOutputStream cos = new CheckedOutputStream(fos, new CRC32());


            // 写入数据
            cos.write(data.getBytes());

            // 关闭
            cos.close();

            // 获取校验和值
            long checksum = cos.getChecksum().getValue();
            System.out.println("Checksum: " + checksum);  // Checksum: 3964322768

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

